package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiU_message;
import cn.com.api.mapper.ApiU_messageMapper;
import cn.com.api.service.ApiU_messageService;
import cn.com.api.service.impl.base.U_messageServiceImplBase;
import cn.com.util.DateUtil;
import cn.com.util.PageInfo;

/**
 * 
 **/
@Service
public class ApiU_messageServiceImpl extends U_messageServiceImplBase implements ApiU_messageService {
	@Autowired
	private ApiU_messageMapper apiU_messageMapper;

	// 新增对象后返回记录ID
	@Override
	public int U_messageInsertion(ApiU_message u_message) {
		return this.apiU_messageMapper.U_messageInsertion(u_message);
	}

	public void addmessage(String touserids, String title, String content, String conn_id, String image, String type)
			throws Exception {
		List<ApiU_message> messages = new ArrayList<ApiU_message>();
		String[] userids = touserids.split(",");
		for (String userid : userids) {
			ApiU_message message = new ApiU_message();
			message.setAdd_time(DateUtil.nowTimeToString());
			message.setConn_id(Integer.valueOf(conn_id));
			message.setContent(content);
			message.setImage(image);
			message.setIs_read(0);
			message.setUser_id(userid);
			message.setTitle(title);
			message.setType(Integer.valueOf(type));
			messages.add(message);
		}
		this.apiU_messageMapper.insertmessageList(messages);
	}

	// Map查询
	@Override
	public List<ApiU_message> findU_messagesByMap(Map param, PageInfo page) {
		return this.apiU_messageMapper.findU_messagesByMapListPage(param, page);
	}

	public List<ApiU_message> findU_messages(String user_id, String type, PageInfo page) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("user_id", user_id);
		params.put("type", type);
		List<ApiU_message> list = new ArrayList<ApiU_message>();
		if ("8".equals(type)) {// 圈子相关的信息
			list = this.apiU_messageMapper.findCircleMessagesByMapListPage(params, page);
		} else if ("4".equals(type)) {// 红包相关
			list = this.apiU_messageMapper.findMessagesListPage(params, page);
		} else if ("7".equals(type)) {// 交易消息
			list = this.apiU_messageMapper.findOrderMessagesListPage(params, page);
		} else if ("6".equals(type) || "9".equals(type)) {// 物流通知 发货提醒
			list = this.apiU_messageMapper.findLogisticsMessagesListPage(params, page);
		} else if ("0".equals(type)) {// 系统消息
			params.clear();
			params.put("type", type);
			list = this.apiU_messageMapper.findMessagesListPage(params, page);
		}  else if ("11".equals(type)) {// 新发现接单提醒
			list = this.apiU_messageMapper.findMessagesListPage(params, page);
		}else {
			list = this.apiU_messageMapper.findU_messagesByMapListPage(params, page);
		}
		return list;
	}

	public void insertmessageList(List<ApiU_message> messagelist) {
		this.apiU_messageMapper.insertmessageList(messagelist);
	}

	public ApiU_message readMessage(String messageid) {
		ApiU_message apiU_message = apiU_messageMapper.findU_messageById(Integer.valueOf(messageid));
		apiU_message.setIs_read(1);
		apiU_messageMapper.updateU_message(apiU_message);
		return apiU_message;
	}

	public ApiU_message findMessagesByMap(Map<String, Object> map) {
		return apiU_messageMapper.findMessagesByMap(map);
	}

	public List<ApiU_message> getSysMessageList() {
		PageInfo pageInfo = new PageInfo();
		pageInfo.setShowCount(100000000);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", 0);
		return apiU_messageMapper.findMessagesListPage(map, pageInfo);
	}
	
	@Override
	public int deleteU_message(Integer id){
		return apiU_messageMapper.deleteU_message(id);
	}

}
